

var distributeSnapArgs={};
distributeSnapArgs.defaultUrl = '/commoditylog/list.json';
distributeSnapArgs.url = distributeSnapArgs.defaultUrl;
distributeSnapArgs.distribute=-1;
distributeSnapArgs.commodity=0;

		
Ext.define("CommoditySnap",{
            extend:"Ext.data.Model",
            fields:['commoditySnapPK.distribute','commoditySnapPK.commodity','name',
						'distributeCommodity','price','onshelf','stock',
						'coversdigest','descriptiondigest','createtime','lastupdatetime']
        });

Ext.define('channel.commodity.CommoditySnapPanel',{
	extend : 'Ext.panel.Panel',
	alias: 'widget.commoditySnapPanel',
	layout : 'border',
	initComponent : function(){
		this.west = Ext.create('Ext.tree.Panel',{
			margins : '2 2 2 2',
			region : 'west',
			title: '菜单',
		    width: 200,
		    tools : [
		    	{name:'refresh',type:'refresh',qtip:'刷新模块树',disabled :true,handler:function(){
		    		this.west.store.load();
		    		this.center.store.removeAll();
		    	},scope:this}
		    ],
		    store: westTreeStore,
		    listeners : {
		    	'afterrender':function(o){
		    		o.store.on('beforeload',function(){this.west.down('[name=refresh]').disable();},this);	
		    		o.store.on('load',function(ds,records,successful){
		    			this.west.down('[name=refresh]').enable();
		    		},this);	
		    	},
		    	'itemclick':function(view, record, htme, index, e){
		    		var distribute = record.get('distribute');
		    		if(distribute != -99){
						distributeSnapArgs.distribute=distribute;
						distributeSnapArgs.commodity = 0;
			    		this.centerStore.load();
		    		}
		    	},scope:this
		    },
		    viewConfig: {
		        plugins: {
	                ptype: 'treeviewdragdrop',
                	appendOnly: true
	            },
	            listeners: {
	                drop: function(node, data, dropRec, dropPosition) {
	                	var targetId = dropRec.get('id');
	                	var dropId = data.records[0].get('id');
	                	this.dropNode(targetId,dropId);
	                },scope:this
	            }
		    }
		});
		
		this.centerStore = Ext.create('Ext.data.Store', {
	        model:"CommoditySnap",
			pageSize: 25,        
	        proxy: {
	            type: 'ajax',
	            actionMethods : 'post',           
	            url : '/commoditysnap/list.json',
	            timeout : 1000*1,           
	            extraParams :distributeSnapArgs,                  
		        reader: {
		        	type: 'json',
  		            root: 'channelResult.result',
  		            totalProperty: 'channelResult.totalCount'
		        }
	        },
	        autoLoad: false,
  	        listeners : {
  	        	'load':function(){
//					store.each(function(record) {
//                		alert(record.get("name"));
//           		});
  	        	},
  	        	'beforeload':function(thiz,options){
  	        	},scope:this
  	        }
	    });
	    
		this.center = Ext.create('Ext.grid.Panel',{
			margins : '2 2 2 0',
			region : 'center',
			loadMask: true,
			selModel : Ext.create('Ext.selection.CheckboxModel', {
		        listeners: {
		            selectionchange: function(sm, selections) {
		                var n = selections.length||0;
						this.center.down('#delBtn').setDisabled(n==0);					
						this.center.down('#editBtn').setDisabled(n!=1);	
		            },scope:this
		        }
		    }),
			columnLines: true,
		    store : this.centerStore,
		    viewConfig: {
		    	stripeRows: true,
		        trackOver: true,
		        emptyText : '<div style="color:#999;margin:5px;">当前没有记录显示<div>',
		        plugins: {
	                ptype: 'gridviewdragdrop',
	                dragGroup: 'modelGridDDGroup',
	                dropGroup: 'modelGridDDGroup'
	            }
		    },    
		    columns: [
		    	{xtype: 'rownumberer',width: 80, sortable: false/*,header:'序号'*/,align:'center'},
				{header: '渠道',  dataIndex: 'commoditySnapPK.distribute',width:40},
		    	{header: '商品',  dataIndex: 'commoditySnapPK.commodity',width:80},
		    	{header: '渠道商品号',dataIndex: 'distributeCommodity',width:120},
				{header: '商品名称',  dataIndex: 'name',width:150},
		        {header: '状态',  dataIndex: 'onshelf',width:80,renderer:function(v){
					return v==1 ? '上架':'<div style="color:#333333;">下架<div>';
					}},
				{header: '价格',  dataIndex: 'price',width:60},
				{header: '库存',  dataIndex: 'stock',width:60},
				{header: '创建时间',  dataIndex: 'createtime',width:140,renderer:function(v){
		              		return Ext.util.Format.date(new Date(parseInt(v)),'Y-m-d h:m:s');
		              	}},
				{header: '最后更新时间',  dataIndex: 'lastupdatetime',width:140,renderer:function(v){
		              		return Ext.util.Format.date(new Date(parseInt(v)),'Y-m-d H:m:s');
		              	}},
				{
		        	xtype: 'actioncolumn',
	                width: 50,
	                align : 'center',
	                items: [{
	                    icon   : '/style/common/images/delete.gif',
						align : 'right',
	                    tooltip: '删除',
	                    stopSelection : false,
	                    handler: function(grid, rowIndex, colIndex) {
	                        grid.up('panel').up('panel').deleteSnap();
	                    },
						getClass: function(v, meta, rec) {
	                            return 'icon_icpn';
	                        }
	                }]
		        }
		    ],
		    tbar : [
		    	{text:'新增',iconCls:'add',scope:this,handler:this.showAddWin},'-',
		    	{text:'修改',iconCls:'edit',disabled:true,itemId:'editBtn',handler:this.showEditWin,scope:this},'-',
		    	{text:'删除',iconCls:'delete',disabled:true,itemId:'delBtn',handler:this.deleteSnap,scope:this},'-',
		    	{text:'查询',iconCls:'search',disabled:false,itemId:'searchBtn',handler:this.showSearchWin,scope:this},'->',
		    	{
	                width: 250,
	                fieldLabel: '商品编号',
	                labelAlign : 'right',
	                labelWidth: 70,
	                xtype: 'searchfield',
	                paramName : 'commodity',
	                store : this.centerStore
	            }	    	
		    ],
	      	 dockedItems:[{
	 	          xtype: 'pagingtoolbar',
	 	          store: this.centerStore,
	 	          dock: 'bottom',
	 	          displayMsg: 'Displaying Exclude Commodity {0} - {1} of {2}',
	 	          emptyMsg: "No Exclude Commodity to display",
	 	          displayInfo: true,
	 	          items:[
	 	                '-', {
	 	                text: 'Show Fail',
	 	                pressed: false,
	 	                enableToggle: true,
	 	                toggleHandler: function(btn, pressed) {
							Ext.Msg.alert('提示',btn.store.getTotalCount());
							
	 	                }
	 	            }]
	 	     }],
		    listeners : {
		    	'afterrender' : function(gp){
		    		gp.view.on('itemdblclick',function(){
		    			var m = gp.getSelectionModel().getSelection();
		    			if(m.length==1){
		    				this.showEditWin();
	    				}
		    		},this);
		    	},scope:this
		    }
		});
		
		this.items = [this.center,this.west];
		this.callParent();
	},
	showAddWin :function(){
		if(!this.addWin){
			this.addWin = Ext.create('channel.commodity.CommoditySnapWin',{title:'新增',fcmp :this});
		}
//		this.centerStore.add(new CommoditySnap({name:'AAAAAA'}));
		this.addWin.show();
	},
	showEditWin :function(){
		if(!this.editWin){
			this.editWin = Ext.create('channel.commodity.CommoditySnapWin',{title:'修改',fcmp :this,_winType:'edit'});
		}
		this.editWin.show();
	},
	showSearchWin :function(){
		if(!this.searchWin){
			this.searchWin = Ext.create('channel.commodity.CommoditySnapSearchWin',{title:'查找',fcmp :this});
		}
		this.searchWin.show();
	},
	deleteSnap : function(){
		Ext.Msg.show({
			icon : Ext.Msg.QUESTION,
			buttons : Ext.Msg.YESNO,
			width : 120,
			scope : this,
			msg : '是否删除选择的快照?',
			fn : function(btn){
				if(btn=='yes'){
					var m = this.center.getSelectionModel().getSelection();
					var distributes = [];
					var commodities = [];
					for(var i=0;i<m.length;i++){
						distributes.push(m[i].get('commoditySnapPK.distribute'));
						commodities.push(m[i].get('commoditySnapPK.commodity'));
					}
					channel.openLink({
						url:'/commoditysnap/del.json',
						params : {distributes:distributes.join(','),commodities:commodities.join(',')},
						scope : this,
						masker : this,
						onSuccess : function(rs){
							this.center.store.load();
						}
					});
				}
			}
		});		
	}
});